<template>
  <div>
    <el-dialog
      :title="title"
      center
      :visible.sync="show"
      width="40%"
      @close="closeBtn"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="学科编号" prop="rid">
          <el-input v-model.trim="form.rid" />
        </el-form-item>
        <el-form-item label="学科名称" prop="name">
          <el-input v-model.trim="form.name" />
        </el-form-item>
        <el-form-item label="学科简称">
          <el-input v-model="form.short_name" />
        </el-form-item>
        <el-form-item label="学科简介">
          <el-input v-model="form.intro" />
        </el-form-item>
        <el-form-item label="学科备注">
          <el-input v-model="form.remark" />
        </el-form-item>
      </el-form>
      <div class="btn">
        <el-button @click="show = false">取消</el-button>
        <el-button type="primary" @click="sureBtn">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { subjectAddPost, subjectEdit } from '@/api/subjectList'
export default {
  props: {
    initList: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      show: false,
      mode: '',
      form: {
        rid: '',
        name: '',
        short_name: '',
        intor: '',
        remark: ''
      },
      rules: {
        rid: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              let bol = false
              if (this.mode === 'add') {
                bol = this.initList.every((item) => {
                  return item.rid !== value
                })
              } else if (this.mode === 'edit') {
                const newArr = this.initList.filter(
                  (item) => item.id !== this.form.id
                )
                bol = newArr.every((item) => {
                  return item.rid !== value
                })
              }
              bol ? callback() : callback(new Error('学科编码重复'))
            }
          }
        ],
        name: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              let bol = []
              if (this.mode === 'add') {
                bol = this.initList.every((item) => {
                  return item.name !== value
                })
              } else if (this.mode === 'edit') {
                const newArr = this.initList.filter(
                  (item) => item.id !== this.form.id
                )
                bol = newArr.every((item) => {
                  return item.name !== value
                })
              }
              bol ? callback() : callback(new Error('学科姓名重复'))
            }
          }
        ]
      }
    }
  },
  methods: {
    sureBtn () {
      this.$refs.form.validate(async (result) => {
        if (result) {
          if (this.mode === 'add') {
            await subjectAddPost(this.form)
            this.$message.success('添加成功')
            this.show = false
            this.$emit('getData')
          } else if (this.mode === 'edit') {
            const res = await subjectEdit(this.form)
            if (res.code === 201) {
              this.$message.error('学科名称不能重复')
            } else {
              this.$message.success('编辑成功')
              this.show = false
              this.$emit('getData')
            }
            // console.log(res)
          }
        }
      })
    },
    closeBtn () {
      this.$refs.form.resetFields()
      this.form = {
        rid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      }
    }
  },
  computed: {
    title () {
      if (this.mode === 'add') {
        return '新增学科'
      } else if (this.mode === 'edit') {
        return '编辑学科'
      } else {
        return ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
  background-color: #05b9fa;
}
::v-deep .el-dialog__title {
  color: #fff;
}
::v-deep .el-dialog__close {
  color: #fff;
}
.btn {
  text-align: center;
  margin-top: 80px;
}
</style>
